﻿using BUS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EC21_EDiamond
{
    public partial class CheckOut : System.Web.UI.Page
    {
        public int tongtien;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //tongtien = 0;
                rpThuongHieu.DataSource = ThuongHieuBUS.DSTH();
                rpThuongHieu.DataBind();

                //if (Session["user"] == null)
                //{
                //    Response.Redirect("~/DangNhap.aspx?ReturnUrl=CheckOut.aspx");
                //}

                if (Session["GioHang"] == null)
                {
                    Response.Redirect("Default.aspx");
                }
                DataTable dt = new DataTable();
                dt = (DataTable)Session["GioHang"];
                rpSoLuongSP.DataSource = dt;
                rpSoLuongSP.DataBind();

                ddlTinh.DataSource = TinhBUS.DsTinh();
                ddlTinh.DataTextField = "TenTinh";
                ddlTinh.DataValueField = "PhiVanChuyen";
                ddlTinh.DataBind();

                lbPhiVanChuyen.Text = "0";
                //if (Request.QueryString["id"] == null)
                //{
                //    lbPhiVanChuyen.Text = "0";
                //}
                //else {
                //    int maTinh = Int32.Parse(Request.QueryString["id"].ToString());
                //    int phiVC = TinhBUS.PhiVanChuyen(maTinh);
                //    lbPhiVanChuyen.Text = phiVC.ToString();
                //}
                DataTable dsGioHang = (DataTable)Session["GioHang"];
                
                for (int i = 0; i < dsGioHang.Rows.Count; i++)
                {
                    tongtien += Int32.Parse(dsGioHang.Rows[i]["TongTien"].ToString());
                }
                tongtien += Int32.Parse(lbPhiVanChuyen.Text.ToString());
                lbTongTien.Text = tongtien.ToString();
                txtTongTien.Text = lbTongTien.Text.ToString();
                Session["TongTam"] = lbPhiVanChuyen.Text.ToString();
           }
        }

        protected void ddlTinh_SelectedIndexChanged(object sender, EventArgs e)
        {

            tongtien += Int32.Parse(lbTongTien.Text.ToString());
            Session["dd1"] = ddlTinh.SelectedItem.Value.ToString();
            lbPhiVanChuyen.Text = Session["dd1"].ToString();
            tongtien = tongtien + Int32.Parse(lbPhiVanChuyen.Text.ToString());
            tongtien -= Int32.Parse(lbPhiVanChuyen.Text.ToString());
            lbTongTien.Text = tongtien.ToString();
            //String url = Request.Url.LocalPath.ToString() + "id=" + ddlTinh.SelectedValue.ToString();
            //Response.Redirect(url);
            txtTongTien.Text = lbTongTien.Text.ToString();
            //Response.Redirect(./);
        }

        protected void cbNguoiDung_CheckedChanged(object sender, EventArgs e)
        {
            if(cbNguoiDung.Checked == true)
            {
                txtDiaChi.ReadOnly = true;
                txtHoTen.ReadOnly = true;
                txtDiDong.ReadOnly = true;
                txtEmail.ReadOnly = true;
            }
            else{
                txtDiaChi.ReadOnly = false;
                txtHoTen.ReadOnly = false;
                txtDiDong.ReadOnly = false;
                txtEmail.ReadOnly = false;
            }
        }

        protected void review_Click(object sender, EventArgs e)
        {
            Session["TenKH"] = txtHoTen.Text.ToString();
            Session["Email"] = txtEmail.Text.ToString();
            Session["DienThoai"] = txtDiDong.Text.ToString();
            Session["DiaChi"] = txtDiaChi.Text.ToString();
            Session["MaTinh"] = ddlTinh.SelectedValue.ToString();
            Session["TongTien"] = lbTongTien.Text.ToString();
            Response.Redirect("https://www.sandbox.paypal.com/cgi-bin/webscr");
        }
    }
}